package com.automannn.cas.practice.casServerWebflow;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.configuration.support.Beans;
import org.apereo.cas.ticket.registry.JpaTicketRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.util.Properties;

/**
 * @author chenkh
 * @time 2022/1/19
 */
@Component
//@Import(JpaTicketRegistry.class)
public class JpaTicketRegistryAdapterConfiguration {

    @Value(value = "${cas.automannn.jpa.packagesToScan:org.apereo.cas.ticket,org.apereo.cas.adaptors.jdbc}")
    private String packagesToScan;

    @Autowired
    private CasConfigurationProperties casConfigurationProperties;

    @Bean
    public HibernateJpaVendorAdapter ticketJpaVendorAdapter(){
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setShowSql(true);
        adapter.setGenerateDdl(true);
        return adapter;
    }

    @Bean
    public ComboPooledDataSource dataSourceTicket() {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        try {
            comboPooledDataSource.setDriverClass(casConfigurationProperties.getTicket().getRegistry().getJpa().getDriverClass());
        } catch (PropertyVetoException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        comboPooledDataSource.setJdbcUrl(casConfigurationProperties.getTicket().getRegistry().getJpa().getUrl());
        comboPooledDataSource.setUser(casConfigurationProperties.getTicket().getRegistry().getJpa().getUser());
        comboPooledDataSource.setPassword(casConfigurationProperties.getTicket().getRegistry().getJpa().getPassword());
        comboPooledDataSource.setInitialPoolSize(casConfigurationProperties.getTicket().getRegistry().getJpa().getPool().getMinSize());
        comboPooledDataSource.setMinPoolSize(casConfigurationProperties.getTicket().getRegistry().getJpa().getPool().getMinSize());
        comboPooledDataSource.setMaxPoolSize(casConfigurationProperties.getTicket().getRegistry().getJpa().getPool().getMaxSize());
        comboPooledDataSource.setMaxIdleTimeExcessConnections(casConfigurationProperties.getTicket().getRegistry().getJpa().getPool().getTimeoutMillis());
        comboPooledDataSource.setCheckoutTimeout((int) Beans.newDuration(casConfigurationProperties.getTicket().getRegistry().getJpa().getPool().getMaxWait()).getSeconds());
        comboPooledDataSource.setAcquireIncrement(casConfigurationProperties.getTicket().getRegistry().getJpa().getPool().getAcquireIncrement());
        comboPooledDataSource.setAcquireRetryAttempts(casConfigurationProperties.getTicket().getRegistry().getJpa().getPool().getAcquireRetryAttempts());
        comboPooledDataSource.setAcquireRetryDelay(casConfigurationProperties.getTicket().getRegistry().getJpa().getPool().getAcquireRetryDelay());
        comboPooledDataSource.setIdleConnectionTestPeriod(casConfigurationProperties.getTicket().getRegistry().getJpa().getPool().getIdleConnectionTestPeriod());
        comboPooledDataSource.setPreferredTestQuery(casConfigurationProperties.getTicket().getRegistry().getJpa().getPool().getConnectionHealthQuery());
        return comboPooledDataSource;
    }


    @Bean
    public LocalContainerEntityManagerFactoryBean ticketEntityManagerFactory(HibernateJpaVendorAdapter adapter,ComboPooledDataSource dataSource)  {
        LocalContainerEntityManagerFactoryBean jpaTransactionManagerFactory= new LocalContainerEntityManagerFactoryBean();
        jpaTransactionManagerFactory.setPersistenceUnitName("ticketEntityManagerFactory");
        jpaTransactionManagerFactory.setJpaVendorAdapter(adapter);
        jpaTransactionManagerFactory.setPackagesToScan(packagesToScan.split(","));
        Properties properties = new Properties();
        properties.put("hibernate.dialect",casConfigurationProperties.getTicket().getRegistry().getJpa().getDialect());
        properties.put("hibernate.hbm2ddl.auto",casConfigurationProperties.getTicket().getRegistry().getJpa().getDdlAuto());
        properties.put("hibernate.jdbc.batch_size",casConfigurationProperties.getTicket().getRegistry().getJpa().getBatchSize());
        jpaTransactionManagerFactory.setJpaProperties(properties);
        jpaTransactionManagerFactory.setDataSource(dataSource);
        return jpaTransactionManagerFactory;
    }

    @Bean
    public EntityManager entityManager(LocalContainerEntityManagerFactoryBean bean){
        return bean.getObject().createEntityManager();
    }




    @Bean
    public JpaTransactionManager ticketTransactionManager(LocalContainerEntityManagerFactoryBean bean){
        JpaTransactionManager jpaTransactionManager= new JpaTransactionManager();
        jpaTransactionManager.setEntityManagerFactory(bean.getObject());

        return jpaTransactionManager;
    }


}
